package com.lwt.gulimall.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author 刘伟涛
 * @Date 2024/3/12 10:47
 **/
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Byte> {

    private Set<Byte> set = new HashSet<>();
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for (int val : vals) {
            set.add(Byte.parseByte(Integer.toString(val)));
        }
    }

    @Override
    public boolean isValid(Byte value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
